#include "AppProcessor.h"
#include "WinAppFile.h"
#include "WinAppProcess.h"
#include "WinAppService.h"

bool AppProcessor::OperFileDownload(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_FILE_DOWNLOAD_REQU pPeq = (LP_FILE_DOWNLOAD_REQU)pRequest;
	LP_FILE_DOWNLOAD_RESP pRes = new FILE_DOWNLOAD_RESP();
	
	pRes->m_nOffSet = pPeq->m_nOffSet;

	if(WinAppFile::FileDownload(pPeq->m_fileName, pPeq->m_nOffSet, pPeq->m_nSize, pRes->m_buf, pRes->m_nSize))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperFileUpload(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_FILE_UPLOAD_REQU pPeq = (LP_FILE_UPLOAD_REQU)pRequest;
	LP_FILE_UPLOAD_RESP pRes = new FILE_UPLOAD_RESP();

	pRes->m_nOffSet = pPeq->m_nOffSet;
	
	if(WinAppFile::FileUpload(pPeq->m_fileName, pPeq->m_nOffSet, pPeq->m_nSize, pPeq->m_buf, pRes->m_nSize))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperCommand(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_COMMAND_REQU pPeq = (LP_COMMAND_REQU)pRequest;
	LP_COMMAND_RESP pRes = new COMMAND_RESP();
	
	if(WinAppProcess::Command(pPeq->m_command))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	
	pResponse = pRes;	
	return bRet;
}

bool AppProcessor::OperDirList(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_DIR_LIST_REQU pPeq = (LP_DIR_LIST_REQU)pRequest;
	LP_DIR_LIST_RESP pRes = new DIR_LIST_RESP();

	const char * pStr = WinAppFile::DirList(pPeq->m_path);

	if(NULL != pStr)
	{
		int nLen = strlen(pStr);
		memcpy(pRes->m_buf, pStr, nLen);
		pRes->m_nLen = nLen;
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;

	}
	
	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperFileExcute(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_FILE_EXCUTE_REQU pPeq = (LP_FILE_EXCUTE_REQU)pRequest;
	LP_FILE_EXCUTE_RESP pRes = new FILE_EXCUTE_RESP();
	
	if(WinAppFile::FileExcute(pPeq->m_fileName, pPeq->m_params))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperFileRename(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_FILE_RENAME_REQU pPeq = (LP_FILE_RENAME_REQU)pRequest;
	LP_FILE_RENAME_RESP pRes = new FILE_RENAME_RESP();

	if(WinAppFile::FileRename(pPeq->m_fileName, pPeq->m_newName))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	
	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperFileDelete(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_FILE_DELETE_REQU pPeq = (LP_FILE_DELETE_REQU)pRequest;
	LP_FILE_DELETE_RESP pRes = new FILE_DELETE_RESP();

	if(WinAppFile::FileDelete(pPeq->m_fileName))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	
	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperDeviceList(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_DEVICE_LIST_REQU pPeq = (LP_DEVICE_LIST_REQU)pRequest;
	LP_DEVICE_LIST_RESP pRes = new DEVICE_LIST_RESP();

	const char * pStr = WinAppFile::DeviceList();

	if(NULL != pStr)
	{
		int nLen = strlen(pStr);
		memcpy(pRes->m_buf, pStr, nLen);
		pRes->m_nLen = nLen;
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

LP_HEAD_PROTOCOL AppProcessor::OperProcessList(LP_HEAD_PROTOCOL & pRequest)
{
	bool bRet = false;
	LP_PROCESS_LIST_REQU pPeq = (LP_PROCESS_LIST_REQU)pRequest;
	LP_PROCESS_LIST_RESP pRes = new PROCESS_LIST_RESP();

	const char * pStr = WinAppProcess::ProcessList();

	if(NULL != pStr)
	{
		int nLen = strlen(pStr);
		pRes->m_buf = (PByte)malloc(nLen);
		memset(pRes->m_buf, 0, nLen);
		memcpy(pRes->m_buf, pStr, nLen);
		pRes->m_nSize = nLen;
		pRes->m_nLen = HEAD_SIZE + 4 + nLen;
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	

	return pRes;
}

bool AppProcessor::OperProcessClose(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_PROCESS_CLOSE_REQU pPeq = (LP_PROCESS_CLOSE_REQU)pRequest;
	LP_PROCESS_CLOSE_RESP pRes = new PROCESS_CLOSE_RESP();
	
	if(WinAppProcess::ProcessClose(pPeq->m_nProcessID))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperServiceList(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_SERVICE_LIST_REQU pPeq = (LP_SERVICE_LIST_REQU)pRequest;
	LP_SERVICE_LIST_RESP pRes = new SERVICE_LIST_RESP();

	const char * pStr = WinAppService::ServiceList();

	if(NULL != pStr)
	{
		int nLen = strlen(pStr);
		memcpy(pRes->m_buf, pStr, nLen);
		pRes->m_nLen = nLen;
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	
	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperServiceStart(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_SERVICE_START_REQU pPeq = (LP_SERVICE_START_REQU)pRequest;
	LP_SERVICE_START_RESP pRes = new SERVICE_START_RESP();
	
	if(WinAppService::ServiceStart(pPeq->m_serviceName))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperServiceStop(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_SERVICE_STOP_REQU pReq = (LP_SERVICE_STOP_REQU)pRequest;
	LP_SERVICE_STOP_RESP pRes = new SERVICE_STOP_RESP();

	if(WinAppService::ServiceStop(pReq->m_serviceName))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	
	pResponse = pRes;	
	return bRet;
}

bool AppProcessor::OperServiceAdd(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_SERVICE_ADD_REQU pReq = (LP_SERVICE_ADD_REQU)pRequest;
	LP_SERVICE_ADD_RESP pRes = new SERVICE_ADD_RESP();
	
	if(WinAppService::ServiceAdd(pReq->m_fileName, pReq->m_serviceName))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}

	pResponse = pRes;
	return bRet;
}

bool AppProcessor::OperServiceDelete(LP_HEAD_PROTOCOL & pRequest, LP_HEAD_PROTOCOL pResponse)
{
	bool bRet = false;
	LP_SERVICE_DELETE_REQU pReq = (LP_SERVICE_DELETE_REQU)pRequest;
	LP_SERVICE_DELETE_RESP pRes = new SERVICE_DELETE_RESP();

	if(WinAppService::ServiceDelete(pReq->m_serviceName))
	{
		pRes->m_nResult = RESULT_SUCCESS;
		bRet = true;
	}
	
	pResponse = pRes;
	return bRet;
}

